package com.caigou.modules.article.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.article.bean.ArticleBean
import com.caigou.modules.article.bean.ArticleItemBean
import com.caigou.modules.article.repository.ArticleRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/11/22
 */
@HiltViewModel
class ArticleViewModel @Inject constructor(private val repository: ArticleRepository) : ViewModel() {

    val articleInfoLiveData = MutableLiveData<Resource<ArticleItemBean>>()
    val articleReplyLiveData = MutableLiveData<Resource<String>>()
    val moreArticleListLiveData = MutableLiveData<Resource<ArticleBean>>()
    val articleCollectLiveData = MutableLiveData<Resource<String>>()

    fun getArticleInfo(userId: String, dataId: Int) {
        articleInfoLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getArticleInfo(userId, dataId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ArticleItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    articleInfoLiveData.value = it
                }
        }
    }

    fun setArticleReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int) {
        articleReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setArticleReply(content, files, senderId, receiverId, dataId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    articleReplyLiveData.value = it
                }
        }
    }

    fun getMoreArticleList(page: Int, limit: Int, userId: String) {
        moreArticleListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMyInfoList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ArticleBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    moreArticleListLiveData.value = it
                }
        }
    }


    fun setArticleCollect(data: Int, userId: String) {
        articleCollectLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setArticleCollect(data, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    articleCollectLiveData.value = it
                }
        }
    }
}